import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

// 画Window10系统按钮
Item{
    anchors.fill: parent
    implicitWidth:135
    Row{
        id: macSystemBar
        spacing: 0
        anchors {
            fill: parent
            verticalCenter: parent.verticalCenter
        }
        //最小化按钮
        Rectangle {
            width: 45
            height: 28
            radius: 0
            color: minCanvas.isMouseEntered? WQTheme.deactiveTitleBackground: "transparent"
            Canvas{
                id:minCanvas 
                property bool isMouseEntered: false
                anchors.fill: parent
                onPaint: {
                    var ctx = getContext("2d");
                    ctx.clearRect(0, 0, width, height);
                    ctx.lineWidth = 1
                    // 计算线条起始和结束的位置 (这里占按钮宽度的60%居中)
                    var startX = width * 0.35
                    var endX = width * 0.65
                    var y = height / 2
                    ctx.beginPath()
                    ctx.moveTo(startX, y)
                    ctx.lineTo(endX, y)
                    // 计算圆心和半径                    
                    ctx.stroke();
                }
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onPressed: function(mouse){
                    // 记下鼠标当前坐标，并标识拖动状态为 true
                    if (mouse.button === Qt.LeftButton) {
                        //mac上window 最小化
                        window.showMinimized()
                    }
                }
                onEntered: {
                    minCanvas.isMouseEntered = true
                }
                onExited: {
                    minCanvas.isMouseEntered = false
                }
                
            }
        }
        //最大化按钮
        Rectangle {
            id: maxCanvas 
            width: 45
            height: 28
            radius: 0
            property bool isMouseEntered: false
            color: maxCanvas.isMouseEntered?WQTheme.deactiveTitleBackground: "transparent" //mac系统上的最小化背景颜色
            Canvas{
                anchors.fill: parent
                onPaint: {
                    var ctx = getContext("2d");
                    ctx.clearRect(0, 0, width, height);
                    // 计算边距，使图形居中
                    var margin = width * 0.35
                    // Windows 10风格通常是一个单一的矩形边框
                    ctx.strokeRect(margin, height*0.35, height/3, height/3)
                    ctx.stroke();
                }
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onPressed: function(mouse){
                    // 记下鼠标当前坐标，并标识拖动状态为 true
                    if (mouse.button === Qt.LeftButton) {
                        console.log("window.windowState=",window.visibility)
                        //如是已经是最大化，则恢复正常大小
                        if(window.visibility == Window.Maximized){
                            window.showNormal()
                        }else{
                            window.showMaximized()
                        }
                    }
                }
                onEntered: {
                    maxCanvas.isMouseEntered = true
                }
                onExited: {
                    maxCanvas.isMouseEntered = false
                }
            }
            
            
        }
        Rectangle {
            width: 45
            height: 28
            radius: 0
            id:closeCanvas
            property bool isMouseEntered: false
            color: closeCanvas.isMouseEntered?"red":"transparent"  
            Canvas{
                id:canvas
                anchors.fill: parent
                onPaint: {
                    var ctx = getContext("2d");
                    ctx.clearRect(0, 0, width, height);
                    ctx.lineWidth = 1.1;
                    var startX = width * 0.35;
                      var startY = height * 0.35;
                    var len = height / 3;
                    ctx.moveTo(startX, startY);
                    ctx.lineTo(startX+len, startY+len);
                    ctx.moveTo(startX, startY+len);
                    ctx.lineTo(startX+len, startY);
                    ctx.strokeStyle = closeCanvas.isMouseEntered ? "white" : "black";  // Windows 10 风格通常使用白色 X
                    ctx.stroke();
                }
            }
            MouseArea {
                anchors.fill: parent
                hoverEnabled: true
                onPressed: function(mouse){
                    //记下鼠标当前坐标，并标识拖动状态为 true
                    if (mouse.button === Qt.LeftButton) {
                        window.close()
                    }
                }
                onEntered: {
                    closeCanvas.isMouseEntered = true
                    canvas.requestPaint()
                }
                onExited: {
                    closeCanvas.isMouseEntered = false
                    canvas.requestPaint()
                }
            }   
        }
    }
}
